/**
 * 线程（用户）
 * 
 */
class Threads {

    /**
     * 线程组
     * 
     * @param {XMLBuilder} parent 
     * @param {{testname: String, config: {testname: string, comments: string, threadNumber: string, loops: string, duration: string}}} config 
     * @returns {XMLBuilder}
     */
    createThreadGroup(parent, config = { testname: "Thread Group", comments: "", threadNumber: "1", loops: "1", duration: "" }) {
        const { testname, comments, threadNumber, loops, duration } = config;
        const jmxThreadGroup = parent.ele("ThreadGroup", { guiclass: "ThreadGroupGui", testclass: "ThreadGroup", testname: testname });
        jmxThreadGroup.ele("stringProp", { name: "TestPlan.comments" }).txt(comments).up()
            .ele("stringProp", { name: "ThreadGroup.num_threads" }).txt(threadNumber).up()
            .ele("intProp", { name: "ThreadGroup.ramp_time" }).txt("1").up()
            .ele("stringProp", { name: "ThreadGroup.duration" }).txt(Boolean(duration) ? duration : "0").up()
            .ele("boolProp", { name: "ThreadGroup.same_user_on_next_iteration" }).txt("true").up()
            .ele("boolProp", { name: "ThreadGroup.scheduler" }).txt(Boolean(duration) ? "true" : "false").up()
            .ele("stringProp", { name: "ThreadGroup.on_sample_error" }).txt("startnextloop").up()
            .ele("elementProp", { name: "ThreadGroup.main_controller", elementType: "LoopController", guiclass: "LoopControlPanel", testclass: "LoopController", testname: "Loop Controller" })
            .ele("stringProp", { name: "LoopController.loops" }).txt(Boolean(duration) ? "-1" : loops).up()
            .ele("boolProp", { name: "LoopController.continue_forever" }).txt("false").up();
        const jmxThreadGroupHashTree = jmxThreadGroup.up().ele("hashTree");
        return jmxThreadGroupHashTree;
    }

}

module.exports = Threads;